<template>
    <div class="addgoods_wrap">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/goods' }">商品列表</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片区域 -->
        <el-card>
            <!-- 提示信息 -->
            <el-alert show-icon center title="添加商品信息" type="info"></el-alert>
            <!-- 步骤条 -->
            <el-steps align-center :active="active - 0" finish-status="success">
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>

            <!-- tbas标签 -->
            <el-form label-position="top" :model="addGoodsForm" :rules="addGoodsFormRules" ref="addGoodsFormRef" label-width="100px" class="demo-ruleForm">
                <el-tabs tab-position="left" v-model="active" :before-leave="handleClick">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="addGoodsForm.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_price">
                            <el-input v-model="addGoodsForm.goods_price"></el-input>
                        </el-form-item>
                        <el-form-item label="商品重量" prop="goods_weight">
                            <el-input v-model="addGoodsForm.goods_weight"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_number">
                            <el-input v-model="addGoodsForm.goods_number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类">
                             <el-cascader :key="selectBug" v-model="selectValue" :options="goodsList" :props="goodsProps" @change="handleChange"></el-cascader>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品参数" name="1">
                        <el-form-item :key="item.cat_id" v-for="(item) in manyArray" :label="item.attr_name">
                            <el-checkbox-group v-model="item.attr_vals">
                                <el-checkbox :key="index" v-for="(attr, index) in item.attr_vals" :label="attr"></el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品属性" name="2">角色管理</el-tab-pane>
                    <el-tab-pane label="商品图片" name="3">定时任务补偿</el-tab-pane>
                    <el-tab-pane label="商品内容" name="4">定时任务补偿</el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>


    </div>
</template>

<script>
export default {
    data() {
        return {
            // 当前被激活的步骤
            active: '0',
            // 添加商品参数
            addGoodsForm: {
                goods_name: '',
                goods_price: 0,
                goods_weight: 0,
                goods_number: 0
            },
            // 商品参数验证规则
            addGoodsFormRules: {
                goods_name: [
                    { required: true, message: '请输入商品名称', trigger: 'blur' }
                ],
                goods_price: [
                    { required: true, message: '请输入商品价格', trigger: 'blur' }
                ],
                goods_weight: [
                    { required: true, message: '请输入商品重量', trigger: 'blur' }
                ],
                goods_number: [
                    { required: true, message: '请输入商品数量', trigger: 'blur' }
                ],
            },
            // 商品分类列表
            goodsList: [],
            // 解决级联选择器的BUG
            selectBug: 0,
            // 级联被选中的值
            selectValue: [],
            // 级联选择器配置
            goodsProps: {
                value: 'cat_id',
                label: 'cat_name',
                children: 'children',
                expandTrigger: 'hover'
            },
            // 参数数组
            manyArray: [],
            onlyArray: [],
        }
    },
    created() {
        this.getGoodsList();
    },
    methods: {
        // tbas标签被点击时调用
        handleClick(activeName, oldActiveName) {
            // 如果没有选中三级分类，阻止下一步
            if (this.selectValue.length != 3) {
                return false;
            }

        },
        // 获取商品分类
        async getGoodsList() {
            const { data: res } = await this.$http.get('categories');
            if (res.meta.status != 200) return this.$message.error('获取商品分类失败');
            this.goodsList = res.data;
            ++this.selectBug;
        },
        // 级联被选中时触发
        async handleChange() {
            // 只能选择三级分类
            if (this.selectValue.length != 3) {
                this.selectValue = [];
                return
            }
             // 获取商品参数
            this.manyArray = await this.getGoodsParams('many');
            this.onlyArray = await this.getGoodsParams('only');
        },
        // 获取商品参数
        async getGoodsParams(params) {
            const { data: res } = await this.$http.get(`categories/${this.selectValue[2]}/attributes`, { params: { sel: params } });
            
            if (params == 'many') {
                res.data.forEach(item => {
                    item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []; 
                });
            }

            return res.data;
        }
    }
}
</script>

<style lang="less" scoped>
.el-steps {
    margin: 20px 0 50px 0;
}
</style>